<template>
  <el-container style="position: relative;">
    <el-main style="padding-left: 15px;">
      <el-scrollbar :style="{height: (getMainContextHeight - 42) + 'px'}" class="custom-scroll">
        <el-form ref="formOrgPaperFilter" :model="formOrgPaper" :size="defaultFormItemSize"
          label-width="100px" label-position="right" @submit.native.prevent>
          <filter-box :item-width="350">
            <el-form-item label="学生名称" prop="formOrgPaperFilter.studentName">
              <el-input class="filter-item" v-model="formOrgPaper.formOrgPaperFilter.studentName"
                :clearable="true" placeholder="输入学生名称模糊查询"/>
            </el-form-item>
            <el-form-item label="创建日期" prop="formOrgPaperFilter.commitTime">
              <date-range class="filter-item" v-model="formOrgPaper.formOrgPaperFilter.commitTime"
                          :clearable="true" :allowTypes="['day']" align="left" range-separator="-"
                          start-placeholder="开始日期" end-placeholder="结束日期"
                          format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss"/>
            </el-form-item>
            <el-button slot="operator" type="default" :plain="true" :size="defaultFormItemSize"
                       @click="onResetFormOrgPaper">重置</el-button>
            <el-button slot="operator" type="primary" :plain="true" :size="defaultFormItemSize"
                       @click="refreshFormOrgPaper(true)">查询</el-button>
            <el-button slot="operator" type="primary" :size="defaultFormItemSize"
                       @click="onFormCreateOrgPaperClick">新建</el-button>
          </filter-box>
        </el-form>
        <el-row>
          <el-col :span="24">
            <el-table ref="orgPaper" :data="formOrgPaper.OrgPaper.impl.dataList" :size="defaultFormItemSize"
                      @sort-change="formOrgPaper.OrgPaper.impl.onSortChange" header-cell-class-name="table-header-gray"
            >
              <el-table-column label="序号" header-align="center" align="center"
                               type="index" width="55px" :index="formOrgPaper.OrgPaper.impl.getTableIndex"/>
              <el-table-column label="学生名称" prop="studentName">
              </el-table-column>
              <el-table-column label="创建时间" prop="commitTime">
                <template slot-scope="scope">
                  <span>{{formatDateByStatsType(scope.row.commitTime, 'day')}}</span>
                </template>
              </el-table-column>
              <el-table-column label="校区" prop="schoolIdDictMap.name">
              </el-table-column>
              <el-table-column label="设备类型" prop="deviceTypeDictMap.name"
              ></el-table-column>
              <el-table-column label="相关文件" min-width="180px">
                <template slot-scope="scope">
                  <a v-for="item in parseUploadData(scope.row.document, {id: scope.row.transId, fieldName: 'document',
                  asImage: false})" :key="item.url" href="javascript:void(0);"
                  @click="downloadFile(item.url, item.name)">{{item.name}}</a>
                </template>
              </el-table-column>
              <el-table-column label="操作" fixed="right">
                <template slot-scope="scope">
                  <el-button @click.stop="onFormEditOrgPaperClick(scope.row)" type="text" :size="defaultFormItemSize">
                    编辑
                  </el-button>
                  <el-button @click.stop="onDeleteClick(scope.row)" type="text" :size="defaultFormItemSize">
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="end" style="margin-top: 10px;">
              <el-pagination
                :total="formOrgPaper.OrgPaper.impl.totalCount"
                :current-page="formOrgPaper.OrgPaper.impl.currentPage"
                :page-size="formOrgPaper.OrgPaper.impl.pageSize"
                :page-sizes="[10,20,50,100]"
                layout="total, prev, pager, next, sizes"
                @current-change="formOrgPaper.OrgPaper.impl.onCurrentPageChange"
                @size-change="formOrgPaper.OrgPaper.impl.onPageSizeChange">
              </el-pagination>
            </el-row>
          </el-col>
        </el-row>
      </el-scrollbar>
    </el-main>
  </el-container>
</template>

<script>
import { mapGetters } from 'vuex';
import { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js';
import { OrgPaperController, DictionaryController } from '@/api';
import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins';
import formEditOrgPaper from '@/views/myOrg/formEditOrgPaper.vue';
import formCreateOrgPaper from '@/views/myOrg/formCreateOrgPaper.vue';

export default {
      name: "formOrgPaper",
      props: {},
      mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
      data () {
        return{
          formOrgPaper:{
            formOrgPaperFilter:{
              studentName: undefined,
              commitTime: []
            },
            formFilterCopy:{
              studentName: undefined,
              commitTime: []
            },
            OrgPaper:{
              impl: new TableWidget(this.loadOrgPaperWidgetData,this.loadOrgPaperVerify,true,false,'commitTime',1)
            },
            isInit: false
          }
        }
      },
      methods:{
        /**
         * 新建
         */
        onFormCreateOrgPaperClick () {
          let params = {};

          this.$dialog.show('新建',formCreateOrgPaper,{
            area: '800px'
          },params).then(res => {
            this.refreshFormOrgPaper();
          }).catch(e => {});
        },

        /**
         * 重置
         */
        onResetFormOrgPaper () {
          this.$refs.formOrgPaperFilter.resetFields();
          this.refreshFormOrgPaper(true);
        },

        /**
         * 编辑
         */
        onFormEditOrgPaperClick (row) {
          alert("编辑:" + row.transId)
          let params = {
            transId: row.transId
          };

          this.$dialog.show('编辑',formEditOrgPaper,{
            area: '800px'
          },params).then(res => {
          }).catch(e => {});
        },

        /**
         * 数据获取函数，返回Promise
         */
        loadOrgPaperWidgetData (params) {
          if (params == null) params = {};
          params = {
            ...params,
            orgPaperDtoFilter: {
              studentName: this.formOrgPaper.formFilterCopy.studentName,
              commitTimeStart: Array.isArray(this.formOrgPaper.formFilterCopy.commitTime) ? this.formOrgPaper.formFilterCopy.commitTime[0] : undefined,
              commitTimeEnd: Array.isArray(this.formOrgPaper.formFilterCopy.commitTime) ? this.formOrgPaper.formFilterCopy.commitTime[1] : undefined,
            }
          };

          return new Promise(((resolve, reject) => {
              OrgPaperController.list(this,params).then(res => {
                resolve({
                  dataList: res.data.dataList,
                  totalCount: res.data.totalCount
                });
              }).catch(e => {
                reject(e);
              })
          }));
        },

        /**
         * 数据获取检测函数，返回true正常获取数据，返回false停止获取数据
         */
        loadOrgPaperVerify () {
          alert('OrgPaperVerify:'+this.formOrgPaper.formOrgPaperFilter.studentName);
          this.formOrgPaper.formFilterCopy.studentName = this.formOrgPaper.formOrgPaperFilter.studentName;
          this.formOrgPaper.formFilterCopy.commitTime = this.formOrgPaper.formOrgPaperFilter.commitTime;
          return true;
        },

        /**
         * 更新数据管理
         * @param reloadData
         */
        refreshFormOrgPaper (reloadData = false) {
          if (reloadData) {
            this.formOrgPaper.OrgPaper.impl.refreshTable(true,1);
          } else {
            this.formOrgPaper.OrgPaper.impl.refreshTable();
          }
          this.formOrgPaper.isInit = true;
        },

        /**
         * 删除
         */
        onDeleteClick (row) {
          let params = {
            id: row.transId
          };
          this.$confirm('是否删除?').then(res => {
            OrgPaperController.delete(this,params).then(res => {
              this.$message.success('删除成功');
              this.formOrgPaper.OrgPaper.impl.refreshTable(false,1);
            }).catch(e => {});
          }).catch(e => {});
        },

        formInit () {
          this.refreshFormOrgPaper();
        }
      },

      computed:{
        ...mapGetters(['getMainContextHeight'])
      },

      mounted () {
        this.formInit();
      }
    }
</script>

<style scoped>
  >>> .el-tree-node__content {
    height: 35px;
  }
</style>
